package com.itheima.test4;

import java.io.*;
import java.net.Socket;

/*
上传文件的 客户端
 */
public class KehuClient {
    public static void main(String[] args) throws IOException {
        //创建客户端对象

        for (int i=0;i<100;i++) {
            Runnable runnable = new Runnable() {
                public void run() {

                    BufferedInputStream bis = null;
                    Socket socket=null;
                    try {
                        socket= new Socket("127.0.0.1", 8888);
                        //创建本地输入流
                        bis = new BufferedInputStream(new FileInputStream("C:\\Users\\Administrator\\Desktop\\美女.jpg"));

                        // 获取输出流
                        OutputStream os = socket.getOutputStream();
                        BufferedOutputStream bos = new BufferedOutputStream(os);
                        //读写
                        int b;
                        while ((b = bis.read()) != -1) {
                            bos.write(b);
                        }
                        //给一个结束标记
                        socket.shutdownOutput();
                        //获取 服务端的响应
                        InputStream is = socket.getInputStream();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                        String s;
                        while ((s=reader.readLine())!=null){
                            System.out.println(s);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        //释放资源
                        if (bis!=null) {
                            try {
                                bis.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        if (socket!=null) {
                            try {
                                socket.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }



                }
            };

            new Thread(runnable).start();
        }


    }
}
